package org.lq.shopping.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.log4j.Logger;
import org.lq.shopping.dao.UserGoodsDao;
import org.lq.shopping.entity.ShopManager;
import org.lq.shopping.entity.UserGoods;
import org.lq.shopping.util.DruidUtil;

public class UserGoodsDaoImpl implements UserGoodsDao {
	
	Logger log = Logger.getLogger(UserGoodsDaoImpl.class);
	QueryRunner qr=new QueryRunner(DruidUtil.getDataSource());
	public int add(UserGoods t) {
		int num = 0 ;
		String sql = "insert into user_goods" + 
				"   (user_id, goodstype_id) values (v_user_id, v_goodstype_id)";
		try {
			num = qr.execute(sql, t.getUser_id(),t.getGoodstype_id());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int update(UserGoods t) {
		int num = 0;
		String sql = "update user_goods  set goodstype_id = ?" + 
				"   where user_id = ? ";
		try {
			num = qr.execute(sql, t.getGoodstype_id(),t.getUser_id());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public List<UserGoods> findAll() {
		List<UserGoods> list = null;
		String sql = "select * from USER_GOODS ";
		try {
			list = qr.query(sql, new BeanListHandler<>(UserGoods.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	


	@Override
	public List<UserGoods> LikeColumnByValue(String columName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getCount() {
		int count = 0;
		String sql = "select count(1) from USER_GOODS where user_id =?";
		try {
			count = qr.query(sql, new ScalarHandler<BigDecimal>()).intValue();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public List<UserGoods> findPage(int pageIndex, int pageSize) {
		String sql = "select * from("
				+ " select rownum r,t.* from ("
				+ " select * from USER_GOODS order by user_id "
				+ ") t where rownum <= ? "
				+ ") where r > ?";
		//最大上限
		int max = pageIndex * pageSize;
		//最小下限
		int min = (pageIndex-1)*pageSize;
		List<UserGoods> list = null;
		try {
			list = qr.query(sql, new BeanListHandler<>(UserGoods.class),max,min);
			log.debug("sql语句："+sql);
		} catch (SQLException e) {
			log.error(e);
		}
		return list;
	}

	@Override
	public List<Integer> findByUserId(int userId) {
		List<Integer> list = null;
		String sql = "select *  from user_goods where user_id = ?";
		try {
			list  = qr.execute(sql,new BeanHandler<>(Integer.class), userId);
			log.debug("sql语句："+sql);
			log.debug("传入的参数值："+userId);

		} catch (SQLException e) {
			log.error(e);
		}
		return list;
	}

	@Override
	public UserGoods findById(int id) {
		return null;
	}
	
	@Override
	public int deleteUserGoods(int userid, int goodStypeid) {
		int u = 0;
		String sql = "delete from user_goods where user_id = ? and  goodstype_id = ?";
		try {
			u = qr.execute(sql,userid,goodStypeid);
			log.debug("sql语句："+sql);
			log.debug("传入的参数值："+userid+goodStypeid);
		} catch (SQLException e) {
			log.error(e);
		}
		return u;
	}

}
